/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.apt.hanhhanh.matrimony.methods;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apt.matrimony.db.ConvertDateTime;
import org.apt.matrimony.db.MyConnection;
import org.apt.matrimony.utils.Tools;

/**
 *
 * @author yoko
 */
public class Inserts {

    public static boolean sendRequest(String fromUser, String text, int toUser) {
        Connection cnn = null;
        PreparedStatement stm = null;
        int fromUserId = -1;
        try {
            fromUserId = selects.getAccountIdByAccountName(fromUser);
            Calendar cal = Calendar.getInstance();
            Date time = cal.getTime();
            String sTime = Tools.convertTime2String(time);
            String sql = "Insert into Requests(Request_Text,Request_Sender_Id,Request_Time,Account_Id) values(?,?,?,?)";
            cnn = MyConnection.openConnection();
            stm = cnn.prepareStatement(sql);
            stm.setString(1, text);
            stm.setInt(2, toUser);
            stm.setString(3, sTime);
            stm.setInt(4, fromUserId);
            int status = stm.executeUpdate();
            if (status == 1) {
                return true;
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            MyConnection.closeConnection(cnn, stm, null);
        }
        return false;
    }

    public static int insertBasicAccount(String username, String pass, String email, String payName, String gender) {

        int insert = 0;
        Connection cnn = null;
        PreparedStatement pst = null;
        String tblName = "Accounts";
        String[] columnName = {"User_Password", "User_Name", "User_Email", "Account_Status", "Account_Role", "Date_Registed", "Login_Count", "Date_Expired", "Gender"};
        String dateReg = ConvertDateTime.getRegisterDate();
        String dateEx = ConvertDateTime.addDate(payName, dateReg);
        String sql = sqlInsertComm(tblName, columnName);

        try {
            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);

            pst.setString(1, pass);
            pst.setString(2, username);
            pst.setString(3, email);
            pst.setBoolean(4, true);
            pst.setString(5, "member");
            pst.setString(6, dateReg);
            pst.setInt(7, 0);
            pst.setString(8, dateEx);
            pst.setString(9, gender);
            insert = pst.executeUpdate();
            if (insert > 0) {
                System.out.println("Thanh cong");
            } else {

                System.out.println("That bai");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return insert;
    }

    public static int insertDetailAccount(String username, String Full_Name, String Date_Of_Birth, String Marital_Status, String Mother_Tongue, int Height, String Contact_Number, int cateID, String Address, int cityID) {


        int insert = 0;
        Connection cnn = null;
        PreparedStatement pst = null;
        String tblName = "Account_Details";
        String[] columnName = {"Account_Id", "Full_Name", "Date_Of_Birth", "Marital_Status", "Mother_Tongue", "Height", "Contact_Number", "City_Id", "Caste_Id", "Address", "Family_Details"};
        //lấy ra AccountID: 

        int account_Id = Updates.getAccountID(username);

        String sql = sqlInsertComm(tblName, columnName);

        try {
            //String date_of_birth = ConvertDateTime.dateConvertToString(Date_Of_Birth);

            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);


            pst.setInt(1, account_Id);
            pst.setString(2, Full_Name);
            pst.setString(3, Date_Of_Birth);
            pst.setString(4, Marital_Status);
            pst.setString(5, Mother_Tongue);
            pst.setInt(6, Height);
            pst.setString(7, Contact_Number);
            pst.setInt(8, cityID);
            pst.setInt(9, cateID);
            pst.setString(10, Address);

            pst.setString(11, "");

            insert = pst.executeUpdate();
            if (insert > 0) {
                System.out.println("Thanh cong");
            } else {

                System.out.println("That bai");
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return insert;
    }

    public static int insertDate_Expired(String username, String pay) {

        // Update tai khoan da co-- them ngày đăng kí + ngày hết hạn

        int update = 0;
        Connection cnn = null;
        PreparedStatement pst = null;
        String tblName = "Accounts";
        String[] columnName = {"Date_Registed", "Date_Expired"};


        String date = ConvertDateTime.getRegisterDate();

        String dateEx = ConvertDateTime.addDate(pay, date);

        String sql = sqlInsertComm(tblName, columnName);

        try {
            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);
            pst.setString(1, date);
            pst.setString(2, dateEx);
            update = pst.executeUpdate();
            if (update > 0) {
                System.out.println("Thanh cong");
            } else {

                System.out.println("That bai");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return update;
    }

    public static int insertCountries(int status, String countryName) {
        int insert = 0;
        Connection cnn = null;
        PreparedStatement pst = null;


        String tblName = "Countries";
        String[] columnName = {"Country_Status", "Country_Name"};

        String sql = sqlInsertComm(tblName, columnName);


        try {
            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);

            pst.setInt(1, status);
            pst.setString(2, countryName);

            insert = pst.executeUpdate();
            if (insert > 0) {
                System.out.println("Thanh cong");
            } else {

                System.out.println("That bai");
            }


        } catch (SQLException ex) {
            Logger.getLogger(Inserts.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return insert;

    }

    // Insert bang Payment Detail
    public static int insertPaymentDetail(String payTypeID, String username) {
        int insert = 0;
        Connection cnn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;


//        String tblName = "Payment_Details";
//        String[] columnName = {"Payment_Type_Id", "Account_Id"};
        int accountID = Updates.getAccountID(username);
        int payType = Integer.parseInt(payTypeID);
        String sql = "Insert into Payment_Details ( Payment_Type_Id , Account_Id) values( " + payType + "," + accountID + ")";



        try {
            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);
            insert = pst.executeUpdate();
            if (insert > 0) {
                System.out.println("Pay Thanh cong");
            } else {

                System.out.println("Pay That bai");
            }


        } catch (SQLException ex) {
            Logger.getLogger(Inserts.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return insert;

    }

    public static int insertReligionDetail(String religionID, String username) {
        int insert = 0;
        Connection cnn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;


        int accountID = Updates.getAccountID(username);
        int religion = Integer.parseInt(religionID);
        String sql = "Insert into Religion_Details (Religion_Type_Id , Account_Details_Id) values(" + religion + ", " + accountID + ")";



        try {
            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);
            insert = pst.executeUpdate();
            if (insert > 0) {
                System.out.println("Insert Religion Detail Success!!!");
            } else {

                System.out.println("Insert Religion Detail Fail!!!");
            }


        } catch (SQLException ex) {
            Logger.getLogger(Inserts.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return insert;

    }

    public static int insertProfessionDetail(String professionID, String username) {
        int insert = 0;
        Connection cnn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;


        int accountDetailID = Updates.getAccountDetailID(username);
        int profession = Integer.parseInt(professionID);
        String sql = "Insert into Professional_Details (Professional_Id , Account_Details_Id) values(" + profession + ", " + accountDetailID + ")";



        try {
            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);
            insert = pst.executeUpdate();
            if (insert > 0) {
                System.out.println("Insert Professional Detail Success!!!");
            } else {

                System.out.println("Insert Professional Detail Fail!!!");
            }


        } catch (SQLException ex) {
            Logger.getLogger(Inserts.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return insert;

    }

    public static int insertExpension(String username, String cityID) {
        int insertExpension = 0;
        int accountDetail = 0;
        int city = Integer.parseInt(cityID);

        Connection cnn = null;
        PreparedStatement pst = null;
        accountDetail = Updates.getAccountDetailID(username);
        String sql = "Insert into Professional_Details (City_Id , Account_Details_Id) values(" + city + ", " + accountDetail + ")";


        try {

            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);

            pst.setInt(1, accountDetail);
            pst.setString(2, cityID);

            insertExpension = pst.executeUpdate();
            if (insertExpension > 0) {
                System.out.println("Thanh cong");
            } else {

                System.out.println("That bai");
            }


        } catch (SQLException ex) {
            Logger.getLogger(org.apt.matrimony.methods.Inserts.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }
        return insertExpension;
    }

    public static String sqlInsertComm(String tblName, String[] columnName) {

        String sql = "Insert into " + tblName + " (";
        if (columnName != null) {
            for (int i = 0; i < columnName.length - 1; i++) {
                sql += " " + columnName[i] + " , ";
            }
            sql += columnName[columnName.length - 1] + ") values(";

            for (int i = 0; i < columnName.length - 1; i++) {
                sql += " ?,";
            }
            sql += "? )";
        }
        System.out.println("SQL - Insert " + tblName + ": " + sql);

        return sql;
    }

    public static String sqlSelectComm(String tblName) {
        String sql = "SELECT * FROM " + tblName;
        return sql;

    }

    public static int insertLikingDetail(String[] liking, String username) {

        Connection cnn = null;
        PreparedStatement pst = null;
        int insertLiking = 0;


        int accountID = Updates.getAccountDetailID(username);

        String sql = "Insert into Liking_Details (Liking_Model_Id , Account_Details_Id) values(?, ?)";



        try {
            cnn = MyConnection.connType4();
            pst = cnn.prepareCall(sql);
            for (int i = 0; i < liking.length; i++) {

                pst.setInt(1, Integer.parseInt(liking[i]));
                pst.setInt(2, accountID);
                insertLiking = pst.executeUpdate();
            }

            if (insertLiking > 0) {
                System.out.println("Insert Liking Thanh cong");
            } else {

                System.out.println("Insert Liking That bai");
            }


        } catch (SQLException ex) {
            Logger.getLogger(Inserts.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            MyConnection.closeConnection(cnn, pst, null);

        }

        return insertLiking;

    }

    public static void main(String[] args) {
        String liking[] = {"1", "4", "6", "9", "11", "10", "13", "14"};
        int ins = insertLikingDetail(liking, "maimai");
        System.out.println(ins);
//     int test = insertProfessionDetail("3", "yoko");
    }
}
